package com.example.leonard.controller;

import com.alibaba.fastjson.JSON;
import com.example.leonard.common.utils.ExcelUtils;
import com.example.leonard.core.domain.Customer;
import com.example.leonard.core.domain.Employee;
import com.example.leonard.service.ICustomerService;
import com.example.leonard.service.IEmployeeService;
import com.example.leonard.service.impl.EmployeeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther heyang
 * @date 2021/4/5 18:02
 */
@Controller
@RequestMapping("/api")
@Api(value = "导出测试")
public class ExcelUploadController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ICustomerService customerService;

    private static final Logger LOGGER = LoggerFactory.getLogger(EmployeeServiceImpl.class);
    private static final HashMap<Object, String> headerMap = new HashMap();

    static {
        headerMap.put("id", "序号");
        headerMap.put("name", "用户姓名");
        headerMap.put("age", "年龄");
        headerMap.put("gender", "性别");
        headerMap.put("tel", "电话");
        headerMap.put("qq", "qq号码");
        headerMap.put("inputTime", "输入时间");
        headerMap.put("status", "状态");
    }

    @RequestMapping(value = "/downloadExcel", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "员工导出", notes = "导出员工的信息")
    public void excelDownload(HttpServletResponse response) {
        List<Customer> customers = customerService.selectAll();
        LOGGER.info("用户的信息->{}", JSON.toJSONString(customers));
        //导出
        ExcelUtils excelUtils = new ExcelUtils(Customer.class, headerMap);
        excelUtils.exportExcel(customers, response);
    }

}
